Repository navigation
Conversation
…ion, which allow multiply-add fusion on all backends by default and turn it off with --fmad=false on CUDA, -ffp-contract=off on ROCm and Metal, the FP_CONTRACT pragmas in OpenCL and Metal sources and correctly rounded half divisions by constants on Metal. Co-Authored-By: Claude <noreply@anthropic.com>
… fusion allowed and turned off, and Metal tests for the correctly rounded half divisions by constants and the fusion and math compile options. Co-Authored-By: Claude <noreply@anthropic.com>
…ich fuse multiply-adds on all GPU backends by default and compute every operation like the host when turned off, and the fast math of Metal. Co-Authored-By: Claude <noreply@anthropic.com>
…urces embedded for runtime compilation and the --fma option. Co-Authored-By: Claude <noreply@anthropic.com>
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Builds on #1693. A new compilation property,
FusedMultiplyAddMode, controls whether the nativecompilers may fuse multiplications and additions into multiply-adds. By default (
Fast), every GPU backend keeps itsfused multiply-adds for the best performance. With
Off, no backend fuses, so every operation rounds like it does onthe host, and Metal divides
Halfvalues by constants with correct rounding.A note on authorship
This PR was done with AI-assistant pair programming (Claude via Claude Code). The commits carry a
Co-Authored-By: Claudetrailer.What's in this PR
1. The
FusedMultiplyAddModeproperty and the--fmaoption (Src/ILGPUC/CompilationProperties.cs,Src/ILGPUC/ProgramOptions/Program.CompilationPropertiesOptions.cs,Src/ILGPUC/IR/ModuleValues/Module.cs)FusedMultiplyAddMode.Fast(the default) andFusedMultiplyAddMode.Off, selected on the command line and inILGPUCompilationOptionswith--fma fast|off. The module carries the mode to code generation.2. Fusion off on every backend (
Src/ILGPUC/Backends/)--fmad=false(CUDA/CUDABackend.cs).-ffp-contract=off(ROCm/ROCmBackend.cs).#pragma OPENCL FP_CONTRACT OFF, which applies to the offline compilers and tosources built at runtime (
OpenCL/CLLanguageConfiguration.cs).#pragma STDC FP_CONTRACT OFF, which also applies to the runtime source compilation,and the Metal compiler runs with
-ffp-contract=off(Metal/MetalLanguageConfiguration.cs,Metal/MetalBackend.cs).Backend.GetFusionFlagadds the native flag;LanguageConfiguration.GetPragmasreceives the fusion mode(
Backend.cs,LanguageConfiguration.cs,CodeGenerator.cs).3. Correctly rounded Half divisions by constants on Metal (
Src/ILGPUC/Backends/ExpressionEmitter.cs,IntrinsicEmitter.cs,Metal/MetalIntrinsicEmitter.cs,Metal/MetalLanguageConfiguration.cs)Halfdivision by a constant that is not a power of two dividesthrough
ilgpu_div_half, which corrects the single-precision quotient by its exact remainder and rounds to thecorrectly rounded
Halfquotient, matchingSystem.Halffor every input.4. Documentation (
Docs/03_Advanced/04_Math-Functions.md,Src/ILGPUC/CompilationProperties.cs)mechanisms, the CPU behaviour and the Metal
Halfdivisions; the XML docs describe both modes and the Metal fastmath.
5. Tests (
Src/ILGPUC.Tests/)HalfPrecisionExecutionTests.HalfDivision_ProducesCorrectOutput(TestPrograms/HalfPrecision/HalfDivision.cs):Halfandfloatdivisions and differences of quotients and products run on every backend with fusionallowed; CPU kernels match the host in every line.
HalfPrecisionExecutionTests.HalfDivisionWithoutFusion_ProducesCorrectOutput: with fusion off, everyresult is bit-exact with
System.Halfandfloaton the host.HalfPrecisionExecutionTests.HalfDivisionByConstants_ProducesCorrectOutput(
TestPrograms/HalfPrecision/HalfDivisionByConstants.cs): everyHalfvalue divided by ten constants matches thehost with fusion off;
HalfDivisionByConstantsNative_ProducesOutputruns the native division in the defaultand fast math modes.
FusedMultiplyAddSourceTests:--fmad=falsefor CUDA,-ffp-contract=offfor ROCm and Metal, the OpenCL and Metalpragmas in the sources and in the sources embedded for runtime compilation, all only with fusion off; unchanged
CUDA, ROCm and CPU sources; the
--fmaoption and its default.MetalSourceTests.HalfDivision_EmitsPreciseMathandMetalSourceTests.MathMode_SelectsCompileOptions: the Metaldivision helper, pragma and compile options for every combination of math and fusion modes.
Depends on
#1693 (based on its head; the base is retargeted once it merges).
Testing
nvcc, NVIDIA GB10TreatWarningsAsErrors🤖 Generated with Claude Code